package com.itcanteen.medical.patient.util;


import com.itcanteen.medical.common.constant.Constant;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;


/**
 * @author 六金
 */
public class SendMsgUtil {

    public static Integer sendMsg(String param1, String phone1) {
        //请求地址 支持http 和 https 及 WEBSOCKET
        String host = "http://smsmsgs.market.alicloudapi.com";
        //后缀
        String path = "/smsmsgs";
        //开通服务后 买家中心-查看AppCodeaee877d03b014083ad67374665e5fa61
        String appCode = "dbc29839e88949e2864a19f38ba5b844";
        String sign = "175622";
        String skin = "12";
        //拼接请求链接
        String urlSend = host + path + "?param=" + param1 +"&phone="+ phone1 +"&sign="+sign +"&skin="+skin;
        try {
            URL url = new URL(urlSend);
            HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection();
            //格式Authorization:APPCODE (中间是英文空格)
            httpURLCon.setRequestProperty("Authorization", "APPCODE " + appCode);
            int httpCode = httpURLCon.getResponseCode();
            if (httpCode == Constant.SendMsgUtilConstant.OK) {
                String json = read(httpURLCon.getInputStream());
                System.out.println("正常请求计费(其他均不计费)");
                System.out.println("获取返回的json:");
                System.out.print(json);
            } else {
                Map<String, List<String>> map = httpURLCon.getHeaderFields();
                String error = map.get("X-Ca-Error-Message").get(0);
                if (httpCode == Constant.SendMsgUtilConstant.ERROR && Constant.SendMsgUtilConstant.APP_CODE_ERROR.equals(error)) {
                    System.out.println("AppCode错误 ");
                } else if (httpCode == Constant.SendMsgUtilConstant.ERROR  && Constant.SendMsgUtilConstant.URL_ERROR.equals(error)) {
                    System.out.println("请求的 Method、Path 或者环境错误");
                } else if (httpCode == Constant.SendMsgUtilConstant.ERROR  && Constant.SendMsgUtilConstant.PARAM_ERROR.equals(error)) {
                    System.out.println("参数错误");
                } else if (httpCode == Constant.SendMsgUtilConstant.UNAUTHORIZED_SERVICE && Constant.SendMsgUtilConstant.UNAUTHORIZED_ERROR.equals(error)) {
                    System.out.println("服务未被授权（或URL和Path不正确）");
                } else if (httpCode == Constant.SendMsgUtilConstant.UNAUTHORIZED_SERVICE && Constant.SendMsgUtilConstant.QUOTA_EXHAUSTED_ERROR.equals(error)) {
                    System.out.println("套餐包次数用完 ");
                } else {
                    System.out.println("参数名错误 或 其他错误");
                    System.out.println(error);
                }
            }
            return httpCode;

        } catch (MalformedURLException e) {
            System.out.println("URL格式错误");
        } catch (UnknownHostException e) {
            System.out.println("URL地址错误");
        } catch (Exception e) {
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>");
            // 打开注释查看详细报错异常信息
             e.printStackTrace();
        }
        return -1;
    }

    /**
     * 读取返回结果
     */
    private static String read(InputStream is) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = br.readLine()) != null) {
            line = new String(line.getBytes(), StandardCharsets.UTF_8);
            sb.append(line);
        }
        br.close();
        return sb.toString();
    }
}
